package it.gma.torero.parsing.token;

import it.gma.torero.parsing.Tokenizer;
import it.gma.torero.parsing.Tokenizer.MalformedException;
import it.gma.torero.parsing.Tokenizer.ReturnType;

public abstract class BynaryOperator extends Operator {

	private Token leftOperand;
	private Token rightOperand;

	BynaryOperator(String sequence) {
		super(sequence);
	}

	@Override
	public Cardinality getCardinality() {
		return Cardinality.BYNARY;
	}

	public Token getLeftOperand() {
		return leftOperand;
	}

	public void setLeftOperand(Token leftOperand) throws MalformedException {
		if (getOperandType() != ReturnType.ANY) {
			if (leftOperand.getReturnType() != ReturnType.RUNTIME && leftOperand.getReturnType() != getOperandType()) {
				throw new Tokenizer.MalformedException("Operand type required " + leftOperand.getReturnType() + " for operator " + getSequence());
			}
		}
		
		
		this.leftOperand = leftOperand;
	}

	public Token getRightOperand() {
		return rightOperand;
	}

	public void setRightOperand(Token rightOperand) throws MalformedException {
		if (getOperandType() != ReturnType.ANY) {
			if (rightOperand.getReturnType() != ReturnType.RUNTIME && rightOperand.getReturnType() != getOperandType()) {
				throw new Tokenizer.MalformedException("Operand type required " + getOperandType() + " for operator " + getSequence());
			}
		}
		
		this.rightOperand = rightOperand;
	}

	
	@Override
	public String toString() {
		return  "(" + leftOperand + " "+  getSequence() +" " + rightOperand + ")";  
	}
	
	
	public  boolean hasOperands() {
		return getRightOperand() != null && getLeftOperand() != null;
	}
	
}
